<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="/css/common.css"> <!-- 引入通用 CSS -->

    <script src="/js/user_list.js"></script>
    <style>
        input[type="text"] {
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            padding: 8px 16px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<!-- 主要内容 -->
<div>
    <h1>用户列表</h1>
    <input type="text" id="usernameSearchInput" placeholder="搜索用户名" class="mb-2">
    <input type="text" id="roleSearchInput" placeholder="搜索角色" class="mb-2">
    <button onclick="searchUsers()" class="ml-2">搜索</button>
    <!-- 标签栏，普通用户不显示 -->
    <div class="tab" th:if="${currentUser.role == 'ADMIN'}">
        <button class="tablinks" onclick="openTab(event, 'admins')" id="defaultOpen" style="color: black">管理者</button>
        <button class="tablinks" onclick="openTab(event, 'normalUsers')" id="normalUser" style="color: black">普通用户</button>
    </div>

    <!-- 管理者用户列表 -->
    <div id="admins" class="tabcontent" th:if="${currentUser.role == 'ADMIN'}">
        <table>
            <thead>
            <tr>
                <th>用户名</th>
                <th>昵称</th>
                <th>角色</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="adminTableBody">
            <tr th:each="user : ${users}" th:if="${user.role == 'ADMIN' and (currentUser.role == 'ADMIN' or (currentUser.role != 'ADMIN' and currentUser.id == user.id))}">
                <td th:text="${user.username}"></td>
                <td th:text="${user.nickname}"></td>
                <td th:text="${user.role}"></td>
                <td>
                    <!-- 编辑权限判断 -->
                    <a th:if="${currentUser.username == 'MR1002' || (currentUser.role == 'ADMIN' and currentUser.id != user.id and user.role != 'ADMIN') || currentUser.id == user.id}"
                       th:href="@{/editUser/{id}(id=${user.id})}">编辑</a>
                    <!-- 删除权限判断 -->
                    <form th:if="${(currentUser.username == 'MR1002' and currentUser.id != user.id) or (currentUser.role == 'ADMIN' and currentUser.id != user.id and user.role != 'ADMIN')}"
                          th:action="@{/deleteUser/{id}(id=${user.id})}" method="post" style="display: inline;">
                        <button type="submit" onclick="return confirmDelete()">删除</button>
                    </form>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
    <!-- 普通用户列表，普通用户直接加载自己的信息 -->
    <div id="normalUsers" class="tabcontent" th:if="${currentUser.role == 'ADMIN'}" style="display: none;">
        <table>
            <thead>
            <tr>
                <th>用户名</th>
                <th>昵称</th>
                <th>角色</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="normalUserTableBody">
            <tr th:each="user : ${users}" th:if="${user.role != 'ADMIN' and (currentUser.role == 'ADMIN' or (currentUser.role != 'ADMIN' and currentUser.id == user.id))}">
                <td th:text="${user.username}"></td>
                <td th:text="${user.nickname}"></td>
                <td th:text="${user.role}"></td>
                <td>
                    <!-- 编辑权限判断 -->
                    <a th:if="${currentUser.username == 'MR1002' || (currentUser.role == 'ADMIN' and currentUser.id != user.id and user.role != 'ADMIN') || currentUser.id == user.id}"
                       th:href="@{/editUser/{id}(id=${user.id})}">编辑</a>
                    <!-- 删除权限判断 -->
                    <form th:if="${(currentUser.username == 'MR1002' and currentUser.id != user.id) or (currentUser.role == 'ADMIN' and currentUser.id != user.id and user.role != 'ADMIN')}"
                          th:action="@{/deleteUser/{id}(id=${user.id})}" method="post" style="display: inline;">
                        <button type="submit" onclick="return confirmDelete()">删除</button>
                    </form>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
    <!-- 普通用户直接显示自己的信息 -->
    <div th:if="${currentUser.role != 'ADMIN'}">
        <table>
            <thead>
            <tr>
                <th>用户名</th>
                <th>昵称</th>
                <th>角色</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="user : ${users}" th:if="${currentUser.id == user.id}">
                <td th:text="${user.username}"></td>
                <td th:text="${user.nickname}"></td>
                <td th:text="${user.role}"></td>
                <td>
                    <!-- 编辑权限判断 -->
                    <a th:if="${currentUser.username == 'MR1002' || (currentUser.role == 'ADMIN' and currentUser.id != user.id and user.role != 'ADMIN') || currentUser.id == user.id}"
                       th:href="@{/editUser/{id}(id=${user.id})}">编辑</a>
                    <!-- 删除权限判断 -->
                    <form th:if="${(currentUser.username == 'MR1002' and currentUser.id != user.id) or (currentUser.role == 'ADMIN' and currentUser.id != user.id and user.role != 'ADMIN')}"
                          th:action="@{/deleteUser/{id}(id=${user.id})}" method="post" style="display: inline;">
                        <button type="submit" onclick="return confirmDelete()">删除</button>
                    </form>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
<script>
    // 默认打开管理者标签页，仅管理员用户有效
    if('${currentUser.role}' === 'ADMIN') {
        document.getElementById("defaultOpen").click();
    }

    function searchUsers() {
        const usernameFilter = document.getElementById('usernameSearchInput').value.toUpperCase();
        const roleFilter = document.getElementById('roleSearchInput').value.toUpperCase();
        const adminTable = document.getElementById('adminTableBody');
        const normalUserTable = document.getElementById('normalUserTableBody');
        const adminRows = adminTable ? adminTable.getElementsByTagName('tr') : [];
        const normalUserRows = normalUserTable ? normalUserTable.getElementsByTagName('tr') : [];

        function filterRows(rows) {
            for (let i = 0; i < rows.length; i++) {
                const cells = rows[i].getElementsByTagName('td');
                const usernameCell = cells[0].textContent || cells[0].innerText;
                const roleCell = cells[2].textContent || cells[2].innerText;

                const usernameMatch = usernameFilter === '' || usernameCell.toUpperCase().indexOf(usernameFilter) > -1;
                const roleMatch = roleFilter === '' || roleCell.toUpperCase().indexOf(roleFilter) > -1;

                if (usernameMatch && roleMatch) {
                    rows[i].style.display = '';
                } else {
                    rows[i].style.display = 'none';
                }
            }
        }

        filterRows(adminRows);
        filterRows(normalUserRows);
    }

    function openTab(evt, tabName) {
        var i, tabcontent, tablinks;
        tabcontent = document.getElementsByClassName("tabcontent");
        for (i = 0; i < tabcontent.length; i++) {
            tabcontent[i].style.display = "none";
        }
        tablinks = document.getElementsByClassName("tablinks");
        for (i = 0; i < tablinks.length; i++) {
            tablinks[i].className = tablinks[i].className.replace(" active", "");
        }
        document.getElementById(tabName).style.display = "block";
        evt.currentTarget.className += " active";
    }
</script>

</body>
</html>